package ejbs;

import java.io.Serializable;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;

import beans.Chamada;
import beans.Usuario;

import interfaces.IChamada;

@Stateless
public class ChamadaEJB implements IChamada, Serializable{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@PersistenceContext(unitName="ProjetoChamadaPU")
	private EntityManager em;

	@Override
	public void insert(Chamada c) {
		em.persist(c);
	}

	@Override
	public void delete(int id) {
		Chamada c;
		c = em.find(Chamada.class, id);
		em.remove(c);

	}

	@Override
	public void update(Chamada c) {
		em.merge(c);
	}

	@Override
	public Chamada select(int id) {
		Query qr = em.createNamedQuery("Chamada.findByIdchamada");
		qr.setParameter("idchamada", id);
		Chamada chamada = (Chamada) qr.getSingleResult();
		
		return chamada;
	}

	@Override
	public List<Chamada> SelectAll() {
		Query p = em.createNamedQuery("Chamada.findAll");
		List l =  p.getResultList();
		return l;
	}
	
	@Override
	public List<Chamada> SelectByUser(Usuario usuario)throws NoResultException{
			Query qr = em.createNamedQuery("Chamada.findByUsuario");
			qr.setParameter("usuario", usuario);
			List l =  qr.getResultList();			
		
		return l;
	}

	@Override
	public long totalChamada() {
		Query qr = em.createNamedQuery("Chamada.total");
		Long t = (Long) qr.getSingleResult();
		return t;
	}

}
